//
//  UIButton+Color.m
//  HX2Car
//
//  Created by Mudmen on 2017/6/19.
//  Copyright © 2017年 Hx2car_mac. All rights reserved.
//

#import "UIButton+Color.h"

@implementation UIButton (Color)
- (void)layoutIfNeeded{
    [super layoutIfNeeded];
   
}
- (void)setBackgroundColors:(NSArray *)colorArray forState:(UIControlState)state gradientType:(GradientType)gradientType
{
    [self.superview layoutIfNeeded];
    UIImage *backImage = [self buttonImageFromColors:colorArray ByGradientType:gradientType];
    [self setBackgroundImage:backImage forState:state];
}

- (UIImage*) buttonImageFromColors:(NSArray *)colors ByGradientType:(GradientType)gradientType{
    if (!colors) {
        return nil;
    }
    NSMutableArray *ar = [NSMutableArray array];
    for(UIColor *c in colors) {
        [ar addObject:(id)c.CGColor];
    }
    UIGraphicsBeginImageContextWithOptions(self.frame.size, YES, 1);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSaveGState(context);
    CGColorSpaceRef colorSpace = CGColorGetColorSpace([[colors lastObject] CGColor]);
    CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)ar, NULL);
    CGPoint start;
    CGPoint end;
    switch (gradientType) {
        case GradientTypeTopToBottom:
            start = CGPointMake(0.0, 0.0);
            end = CGPointMake(0.0, self.frame.size.height);
            break;
        case GradientTypeLeftToRight:
            start = CGPointMake(0.0, 0.0);
            end = CGPointMake(self.frame.size.width, 0.0);
            break;
        case GradientTypeUpleftTolowRight:
            start = CGPointMake(0.0, 0.0);
            end = CGPointMake(self.frame.size.width, self.frame.size.height);
            break;
        case GradientTypeUprightTolowLeft:
            start = CGPointMake(self.frame.size.width, 0.0);
            end = CGPointMake(0.0, self.frame.size.height);
            break;
        default:
            break;
    }
    CGContextDrawLinearGradient(context, gradient, start, end, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    CGGradientRelease(gradient);
    CGContextRestoreGState(context);
    CGColorSpaceRelease(colorSpace);
    UIGraphicsEndImageContext();
    return image;
}

- (UIView *)insterShadowView:(UIColor*)shadowColor shadowWidth:(CGFloat)shadowWidth{
    UIView *shadowView = [[UIView alloc]initWithFrame:self.frame];
    
    shadowView.backgroundColor = self.backgroundColor;
    
    shadowView.layer.shadowColor = shadowColor.CGColor;
    
    shadowView.layer.shadowOffset = CGSizeMake(0, 3);
    
    shadowView.layer.shadowOpacity = 0.6;
    
    shadowView.layer.shadowRadius = shadowWidth;
    shadowView.layer.cornerRadius = self.frame.size.height/2;
    
    shadowView.clipsToBounds = NO;
  
    [[self superview] insertSubview:shadowView belowSubview:self];
  
    return shadowView;
}

- (UIView *)insterOtherShadowView:(UIColor*)shadowColor shadowWidth:(CGFloat)shadowWidth cornerRadius:(CGFloat)cornerRadius{
    UIView *shadowView = [[UIView alloc]initWithFrame:self.frame];
    
    shadowView.backgroundColor = self.backgroundColor;
    
    shadowView.layer.shadowColor = shadowColor.CGColor;
    
    shadowView.layer.shadowOffset = CGSizeMake(0, 0);
    
    shadowView.layer.shadowOpacity = 0.4;
    
    shadowView.layer.shadowRadius = shadowWidth;
    shadowView.layer.cornerRadius = cornerRadius;
    
    shadowView.clipsToBounds = NO;
    
    [[self superview] insertSubview:shadowView belowSubview:self];
    
    return shadowView;
}

@end
